package cse326.fuellog;

import cse326.fuellog.MainActivity.vehicleEntry;
import android.content.ContentValues;
import android.database.sqlite.SQLiteDatabase;
import android.util.Log;

public class Vehicle
{
	private long dbID;
	private SQLiteDatabase db;
	private int year;
	private String make;
	private String model;
	private String color;
	private double averageMPG;

	/**
	 * Creates a new vehicle and adds it to the database.
	 * 
	 * @param db
	 *           Database to use
	 * @param year
	 *           Year to set
	 * @param make
	 *           Make to set
	 * @param model
	 *           Model to set
	 * @param color
	 *           Color to set
	 */
	public Vehicle(int year,String make,String model,String color)
	{
		this.year = year;
		this.make = make;
		this.model = model;
		this.color = color;
		this.averageMPG = 0.0;
		this.db = null;
		this.dbID = -1;
	}

	/**
	 * Creates a new vehicle with safe (invalid) values.
	 */
	public Vehicle()
	{
		this.year = 0;
		this.make = null;
		this.model = null;
		this.color = null;
		this.averageMPG = 0.0;

	}

	/**
	 * Returns the internal id given by the database. Just in case we need it.
	 * 
	 * @return the dbID
	 */
	public long getDbID()
	{
		return dbID;
	}

	/**
	 * Sets the identifier used by the database
	 * 
	 * @param dbID
	 *           id in the database
	 */
	public void setDbID(long dbID)
	{
		this.dbID = dbID;
	}

	/**
	 * @return the year of the vehicle
	 */
	public int getYear()
	{
		return year;
	}

	/**
	 * @param year
	 *           the year of the vehicle to set
	 */
	public void setYear(int year)
	{
		this.year = year;
	}

	/**
	 * @return the make of the vehicle
	 */
	public String getMake()
	{
		return make;
	}

	/**
	 * @param make
	 *           the make of the vehicle to set
	 */
	public void setMake(String make)
	{
		this.make = make;
	}

	/**
	 * @return the model of the vehicle
	 */
	public String getModel()
	{
		return model;
	}

	/**
	 * @param model
	 *           the model of the vehicle to set
	 */
	public void setModel(String model)
	{
		this.model = model;
	}

	/**
	 * @return the color of the vehicle
	 */
	public String getColor()
	{
		return color;
	}

	/**
	 * @param color
	 *           the color of the vehicle to set
	 */
	public void setColor(String color)
	{
		this.color = color;
	}

	/**
	 * @return the averageMPG of the vehicle
	 */
	public double getAverageMPG()
	{
		return averageMPG;
	}

	/**
	 * @param averageMPG
	 *           the averageMPG of the vehicle to set
	 */
	public void setAverageMPG(double averageMPG)
	{
		this.averageMPG = averageMPG;
	}

	/**
	 * Gets the pointer to the database that this vehicle is saved in.
	 * 
	 * @return database pointer
	 */
	public SQLiteDatabase getDB()
	{
		return this.db;
	}

	/**
	 * Sets the database pointer that this data came from
	 * 
	 * @param db
	 */
	public void setDB(SQLiteDatabase db)
	{
		this.db = db;
	}

	/**
	 * Updates the database with the (new) data in this instance.
	 */
	public void updateDB()
	{
		if (this.db == null)
		{
			Log.e("Vehicle.updateDB","Tried to update DB without setting the identifier first.");
			return;
		}

		ContentValues newValues = new ContentValues();
		newValues.put(vehicleEntry.COLUMN_NAME_YEAR,this.year);
		newValues.put(vehicleEntry.COLUMN_NAME_MAKE,this.make);
		newValues.put(vehicleEntry.COLUMN_NAME_MODEL,this.model);
		newValues.put(vehicleEntry.COLUMN_NAME_COLOR,this.color);

		String selection = vehicleEntry._ID + " LIKE ?";
		String[] selectionArgs = {String.valueOf(this.dbID)};

		this.dbID = db.update(vehicleEntry.TABLE_NAME,newValues,selection,selectionArgs);

	}

	/**
	 * Adds the vehicle to the database. Must have called setDB first.
	 */
	public void addToDB()
	{
		if (this.db == null)
		{
			Log.e("Vehicle.addToDB","Tried to add to DB without setting the identifier first.");
			return;
		}
		ContentValues newValues = new ContentValues();
		newValues.put(vehicleEntry.COLUMN_NAME_YEAR,this.year);
		newValues.put(vehicleEntry.COLUMN_NAME_MAKE,this.make);
		newValues.put(vehicleEntry.COLUMN_NAME_MODEL,this.model);
		newValues.put(vehicleEntry.COLUMN_NAME_COLOR,this.color);

		this.dbID = db.insert(vehicleEntry.TABLE_NAME,null,newValues);
	}

}
